package dao;

import java.sql.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

/**
 * Created by Xtra on 3/26 0026.
 */
public class JDBCConn {

    public Connection conn = null;
    public void getConnection(String url, String user, String password) {
        String m_Driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        try {
            Class.forName(m_Driver);
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return;
    }

    public String  executeQuery(String query) {
        if (conn == null || query == null || query.isEmpty()) {
            return null;
        }
        Statement m_Statement = null;
        ResultSet m_ResultSet = null;
        String ItemValue="";
        try {
            m_Statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            m_ResultSet = m_Statement.executeQuery(query);
            while (m_ResultSet.next()) {
                ItemValue=m_ResultSet.getString(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (m_ResultSet != null)
                    m_ResultSet.close();
                if (m_Statement != null)
                    m_Statement.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
                return "";
            }
        }

        return ItemValue;
    }

    public void close() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String url = "jdbc:sqlserver://192.168.0.60:1433;databaseName=nurse";
        String user = "nurse";
        String password = "nurse123";
        String sql1 = "select a.c_content from t_mb_emr_docValue a left JOIN t_mb_emr_items b on a.c_item=b.c_itemID where a.c_document='402886f45990e71a015990e9cca30000' AND  c_displayTitle='年龄阶段'";
        JDBCConn JDBCConn = new JDBCConn();

        JDBCConn.getConnection(url, user, password);
        String s = JDBCConn.executeQuery(sql1);
        JDBCConn.close();
        System.out.println(s);
    }
}
